package com.estun.backroundManagementService.modules.material.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.estun.backroundManagementService.aop.operationLog.Log;
import com.estun.backroundManagementService.aop.operationLog.LogConstants;
import com.estun.backroundManagementService.domain.BasePageRequest;
import com.estun.backroundManagementService.domain.BaseResponse;
import com.estun.backroundManagementService.domain.factoryMes.request.ManualMaterialCallingRequest;
import com.estun.backroundManagementService.modules.material.model.ManualMaterialCalling;
import com.estun.backroundManagementService.modules.material.service.ManualMaterialCallingService;
import com.estun.backroundManagementService.utils.QueryWrapperUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/ManualMaterialCalling")
@RequiredArgsConstructor
public class ManualMaterialCallingController {

    private final ManualMaterialCallingService manualMaterialCallingService;


    //分页查询物料信息
    @PostMapping("/page")
    public BaseResponse page(@RequestBody BasePageRequest<ManualMaterialCalling> pageRequest) {
        Page<ManualMaterialCalling> page = new Page<>(pageRequest.getPage(), pageRequest.getSize());
        QueryWrapper<ManualMaterialCalling> queryWrapper = QueryWrapperUtil.buildQueryWrapper(pageRequest.getQuery());
        Page<ManualMaterialCalling> page1 = manualMaterialCallingService.page(page, queryWrapper);
        return BaseResponse.success(page1);
    }


    // 根据ID获取物料信息
    @GetMapping("/{id}")
    public BaseResponse getMaterialById(@PathVariable String id) {
        return BaseResponse.success(manualMaterialCallingService.getById(id));
    }

    // 添加物料信息
    @PostMapping("/add")
    @Log(module = "手动叫料 工厂ems同步", type = LogConstants.INSERT, desc = "手动叫料 工厂ems同步")
    public BaseResponse addMaterial(@RequestBody ManualMaterialCalling material) {
        return BaseResponse.success(manualMaterialCallingService.save(material));
    }

    // 更新物料信息
    @PutMapping("/update")
    @Log(module = "手动叫料 更新", type = LogConstants.UPDATE, desc = "手动叫料 更新")
    public BaseResponse updateMaterial(@RequestBody ManualMaterialCalling material) {
        return BaseResponse.success(manualMaterialCallingService.updateById(material));
    }

    // 删除物料信息
    @DeleteMapping("/delete/{id}")
    @Log(module = "手动叫料 删除", type = LogConstants.DELETE, desc = "手动叫料 删除")
    public BaseResponse deleteMaterial(@PathVariable String id) {
        return BaseResponse.success(manualMaterialCallingService.removeById(id));
    }


    @ApiOperation("手动叫料")
    @PostMapping(value = "/manualMaterialCalling")
    @ResponseBody
    public BaseResponse manualMaterialCalling(@RequestBody ManualMaterialCallingRequest req) {
        return manualMaterialCallingService.manualMaterialCalling(req);
    }

}
